<?php
namespace app\admin\controller;

use app\common\model\Act as ActModel;
use app\common\model\Draw as DrawModel;
use app\common\model\Company as CompanyModel;
use app\common\model\Manager as ManagerModel;
/**
 * 提现控制器
 * Class Draw
 * @package app\index\controller
 */
class Draw extends Base
{
    /**
     * [getIndex  提现列表]
     * @param key:1项目 2寺院 3法师
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getIndex()
    {
        $data = input('param.');
        $model = new DrawModel();
        $user = $this->user;
        if(!empty($data['findone'])){
            try{
                $list = $model->findData($data['id']);
            }catch(\Exception $e){
                return admin_json(-1, '获取失败'.$e->getMessage());
            }
            writelog('查看提现--'.$data['id']);
            return admin_json(1, '获取成功',$list);
        }
        $this->getPageAndSize($data);
        $map = [];
        if (is_array($data)) {
            if(!empty($data['key'])){
                if ($data['key'] == 1)
                    $map['aa.title'] = ['like', "%" . $data['con'] . "%"];
                elseif($data['key'] == 2)
                    $map['ac.comname'] = ['like', "%" . $data['con'] . "%"];
                elseif($data['key'] == 3)
                    $map['am.username|am.tel'] = ['like', "%" . $data['con'] . "%"];
                if(!empty($data['dtype'])){
                    if($data['dtype'] == 1){
                        $map['d.dtype'] = 1;
                    }elseif ($data['dtype'] == 2){
                        $map['d.dtype'] = 0;
                    }
                }

                if(!empty($data['states'])){
                    if($data['states'] == 1){
                        $map['d.states'] = 1;
                    }else{
                        $map['d.states'] = ['neq',1];
                    }
                }
                if(!empty($data['start']) && !empty($data['end'])){
                    $map['d.addtime'] = ['between',[strtotime($data['start']),strtotime($data['end'])]];
                }
            }else{
                if(!empty($data['dtype'])){
                    if($data['dtype'] == 1){
                        $map['dtype'] = 1;
                    }elseif ($data['dtype'] == 2){
                        $map['dtype'] = 0;
                    }
                }

                if(!empty($data['states'])){
                    if($data['states'] == 1){
                        $map['states'] = 1;
                    }else{
                        $map['states'] = ['neq',1];
                    }
                }
                if(!empty($data['start']) && !empty($data['end'])){
                    $map['addtime'] = ['between',[strtotime($data['start']),strtotime($data['end'])]];
                }
            }
            if(!empty($data['key'])){
                try{
                    $count = $model->getCount($map,$data['key']);//满足条件的总数
                    $allpage = intval(ceil($count / $this->size));//总页数
                    $lists = $model->getlist($map,$this->from, $this->size,$data['key']);
                    foreach($lists as $k=>$v){
                        if(!empty($v['comid'])){
                            $cmodel = new CompanyModel();
                            $company = $cmodel->findData($v['comid']);
                            if(!empty($company)){
                                $lists[$k]['comname'] = $company['comname'];
                            }
                        }
                        if(!empty($v['manager_id'])){
                            $mmodel = new ManagerModel();
                            $member = $mmodel->findData($v['manager_id']);
                            if(!empty($member)){
                                $lists[$k]['tel'] = $member['tel'];
                                $lists[$k]['username'] = $member['username'];
                            }
                        }
                        if(!empty($v['item_id'])){
                            $amodel = new ActModel();
                            $active = $amodel->findData($v['item_id']);
                            if(!empty($active)){
                                $lists[$k]['actname'] = $active['title'];
                            }
                        }
                    }
                }catch(\Exception $e){
                    return admin_json(-1, '获取失败'.$e->getMessage());
                }
            }else{
                try{
                    $count = $model->getDrawCount($map);//满足条件的总数
                    $allpage = intval(ceil($count / $this->size));//总页数
                    $lists = $model->getDrawByCondition($map,$this->from, $this->size);
                    foreach($lists as $k=>$v){
                        if(!empty($v['comid'])){
                            $cmodel = new CompanyModel();
                            $company = $cmodel->findData($v['comid']);
                            if(!empty($company)){
                                $lists[$k]['comname'] = $company['comname'];
                            }
                        }
                        if(!empty($v['item_id'])){
                            $amodel = new ActModel();
                            $active = $amodel->findData($v['item_id']);
                            if(!empty($active)){
                                $lists[$k]['actname'] = $active['title'];
                            }
                        }
                        if(!empty($v['manager_id'])){
                            $mmodel = new ManagerModel();
                            $member = $mmodel->findData($v['manager_id']);
                            if(!empty($member)){
                                $lists[$k]['tel'] = $member['tel'];
                                $lists[$k]['username'] = $member['username'];
                            }
                        }
                    }
                }catch(\Exception $e){
                    return admin_json(-1, '获取失败'.$e->getMessage());
                }
            }
        }

        writelog('查看提现明细列表',0,$user);
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [index  提现审核]
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postEdit()
    {
        $param = input('post.');
        $model = new DrawModel();
        $user = $this->user;
        try {
            $model->editData($param);
        } catch (\Exception $e) {
            return admin_json(-1, '修改失败');
        }

        writelog('审核提现'.$param['id'],1,$user);

        return admin_json(1, '修改成功');
    }
}